package com.lqian.controller;

import java.util.List;
import java.util.Map;

import javax.jws.WebParam.Mode;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lqian.pojo.Depart;
import com.lqian.pojo.Employee;
import com.lqian.service.depart.DepartService;
import com.lqian.service.emp.EmployeeServiceImpl;
import com.lqian.utils.PageUtil;

@Controller
public class EmployeeController {

	@Autowired
	EmployeeServiceImpl employeeService;
	
	
	@Autowired
	private DepartService departService;


	@RequestMapping("/login.do")
	public String getEmployee(String ename, String epass,Model model,HttpSession session){
		Employee emp = employeeService.login(ename, epass);
		session.setAttribute("emp", emp);
		if(StringUtils.isEmpty(emp)){
			return "login.html";
		}
		model.addAttribute("emp", emp);
		return "index.jsp";
	}

	@RequestMapping("/myInfo.do")
	public String getEmployee(Model model,HttpSession session){
		//��ȡ��ǰ�û���¼��eid
		Employee employee = (Employee) session.getAttribute("emp");
		//��ǰ��¼���û�eid
		Integer eid = employee.getEid();
		model.addAttribute("my", employeeService.getEmployeeWithDept(eid));
		return "myinfo.jsp";
	}


	@RequestMapping("/toLogin.do")
	public String toLogin(){
		return "login.html";
	}


	@RequestMapping("/loginOut.do")
	public String toLogin(HttpSession session){
		session.removeAttribute("emp");
		return "redirect:login.html";
	}


	@RequestMapping("/list.do")
	public String list(){
		List<Employee> allEmployee = employeeService.getAllEmployee();
		return "login.html";
	}


	@RequestMapping("/updatePwd.do")
	public String updatePwd(String password,String rpassword,HttpSession session){
		System.out.println(password);
		System.out.println(rpassword);
		if(password.equals(rpassword) && password.length()>=6){
			//�õ��û�eid
			Employee employee = (Employee)session.getAttribute("emp");
			Integer eid = employee.getEid();
			//���
			employeeService.updatePwd(password, eid);
			System.out.println("success!");
		}
		return "index.jsp";
	}

	@RequestMapping("/user.do")
	public String user(Model model,Integer curPage) {

		if(curPage == null) {
			curPage=1;
		}
		//Integer count 总数据数
		int count = employeeService.count();
		System.out.println("总数"+count);	
		//Integer pageSize 页面大小
		int pageSize=10;
		//Integer curPage 当前页
		PageUtil page=new PageUtil(count, pageSize, curPage);		
		//List<Employee> list=service.findAllEmp();
		List<Employee> list=employeeService.findAllEmpByPage(page);
		model.addAttribute("list", list);
		model.addAttribute("page", page);
		return "user.jsp";
	}

	/**
	 * 根据id查找用户
	 * @param eid
	 * @param model
	 * @return
	 */
	@RequestMapping("/findUserByEid.do")
	public String findUserByEid(Integer eid,Model model) {

		System.out.println(eid);

		//操作数据库
		Map<String, Object> map=employeeService.findUserByEid(eid);		

		//员工信息
		Employee emp=(Employee) map.get("emp");

		//部门列表
		List<Depart> departList=(List<Depart>) map.get("departList");

		//传递数据
		model.addAttribute("emp", emp);
		model.addAttribute("departList", departList);

		return "editUser.jsp";
	}

	/**
	 * 更新编辑用户
	 * @param emp
	 * @return
	 */
	@RequestMapping("/updateUser.do")
	public String updateUser(Employee emp) {

		System.out.println(emp.getEid());
		System.out.println(emp.getDid());
		System.out.println(emp.getEsex());

		//操作数据库
		int num=employeeService.updateUser(emp);

		//返回试图
		return "redirect:user.do";
	}

	/**
	 * 逻辑删除
	 * @param did
	 * @return
	 */
	@RequestMapping("/deleteEmp.do")
	public String deleteDepart(Integer eid) {
		Integer num = employeeService.deleteEmployee(eid);
		return "redirect:user.do";
	}
	/**
	 * 恢复
	 * @param did
	 * @return
	 */
	@RequestMapping("/resEmp.do")
	public String resDepart(Integer eid) {
		Integer num=employeeService.resEmployee(eid);
		return "redirect:user.do";
	}
	
	
	@RequestMapping("/addEmp.do")
	public String addEmp(Employee employee){
		System.out.println(employee.toString());
		employeeService.addEmployee(employee);
		return "redirect:user.do";
	}
	
	@RequestMapping("/getAddDept.do")
	public String getAddDept(Model model){
		
		//得到所有部门列表信息
		List<Depart> departList = departService.getAllDepart();
		model.addAttribute("departList",departList);
		
		return "addUser.jsp";
		
	}


}
